package com.example.demo.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;

import java.io.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class Utils {

    public static String PREFIX;

    private static String wavedata = "\\waves" + DateUtil.format(new Date(), "yyyyMMdd") + "\\";

//    private static String waveimg = "\\waveimg" + DateUtil.format(new Date(), "yyyyMMdd") + "\\";

    private static final String JSpath = "\"D:\\temp\\phantomjs-2.1.1-windows\\echarts-convert\\echarts-convert.js\"";

    public static String getPREFIX() {
        return PREFIX;
    }

    public static void setPREFIX(String PREFIX) {
        Utils.PREFIX = PREFIX;
    }

    /**
     * 生成图片
     *
     * @param options
     * @return
     */
    public static String generateEChart(String options, String filename) {
        String dataPath = writeFile(options, filename);
        String path = PREFIX + wavedata + filename + ".png";
        try {
            File file = new File(path);     //文件路径
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            String cmd = "D:\\temp\\phantomjs-2.1.1-windows\\bin\\phantomjs " + JSpath + " -infile " + dataPath + " -outfile " + path;//生成命令行
            System.out.println(cmd);
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return path;
    }

    /**
     * 生成数据
     *
     * @param options
     * @return
     */
    public static String writeFile(String options, String filename) {
        String dataPath = PREFIX + wavedata + filename + ".json";
        try {
            /* option写入文本文件 用于执行命令*/
            File writename = new File(dataPath);
            if (!writename.exists()) {
                File dir = new File(writename.getParent());
                dir.mkdirs();
                writename.createNewFile(); //
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            out.write(options);
            out.flush(); // 把缓存区内容压入文件
            out.close(); // 最后关闭文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataPath;
    }


    /**
     * 导出csv
     *
     * @param filename
     * @param lines
     */
    public static void writeCSV(String filename, String[]... lines) {
        String filePath = PREFIX + wavedata + filename + ".csv";
        //指定路径和编码
        CsvWriter writer = null;
        try {
            writer = CsvUtil.getWriter(filePath, CharsetUtil.CHARSET_GBK);
            //按行写出
            writer.write(Collections.singleton(lines));
        } catch (IORuntimeException e) {
            e.printStackTrace();
        } finally {
            assert writer != null;
            writer.close();
        }
    }

    /**
     * 导出csv
     *
     * @param filename
     * @param lines
     */
    public static void writeTxT(String filename, List lines) {
        String filePath = PREFIX + wavedata + filename + ".csv";
        cn.hutool.core.io.file.FileWriter fileWriter = new cn.hutool.core.io.file.FileWriter(filePath);
        fileWriter.writeLines(lines);
    }


    /**
     * 解析blob数据
     *
     * @param in
     * @return
     * @throws IOException
     */
    public static String getBlob(InputStream in) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[4096];
        int count = -1;
        while ((count = in.read(data, 0, 4096)) != -1)
            outStream.write(data, 0, count);
        data = null;
        String result = new String(outStream.toByteArray(), "gb2312");
        return result;
    }

}
